package cn.edu.sdjzu.xg.eagle.dao;

import cn.edu.sdjzu.xg.eagle.entity.User;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.util.CommonDao;
import cn.edu.sdjzu.xg.eagle.util.JdbcHelper;
import cn.edu.sdjzu.xg.eagle.util.Pagination;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashSet;

public class UserDao {
    private static UserDao instance = new UserDao();

    private UserDao() {}

    public static UserDao getInstance() {
        return instance;
    }

    // 添加用户方法，接收Connection和User对象
    public static boolean add(Connection connection, User user) throws SQLException, BysjException {
        String sql_insert = "INSERT INTO user (username, password) VALUES (?, ?)";
        PreparedStatement pstmt_insert =
                connection.prepareStatement(sql_insert);
        pstmt_insert.setString(1, user.getUsername());
        pstmt_insert.setString(2, user.getPassword());
        int rowAffected = pstmt_insert.executeUpdate();
        return rowAffected > 0;
    }

    // 查找所有用户方法，支持分页
    public Collection<User> findAll(Pagination pagination) throws SQLException, BysjException {
        Collection<User> desiredUsers = new HashSet<>();
        Connection connection = JdbcHelper.getConn();
        StringBuilder sql_select = new StringBuilder("SELECT * FROM User ");
        int totalNum = CommonDao.getInstance().count(connection, "User");
        if (pagination != null) {
            String paginationClause = pagination.toLimitClause(totalNum);
            sql_select.append(paginationClause);
        }
        PreparedStatement pstmt_select_all = connection.prepareStatement(sql_select.toString());
        ResultSet resultSet = pstmt_select_all.executeQuery();
        while (resultSet.next()) {
            Long id = resultSet.getLong("id");
            String username = resultSet.getString("username");
            String password = resultSet.getString("password");
            User desiredUser = new User();
            desiredUsers.add(desiredUser);
        }
        JdbcHelper.close(resultSet, pstmt_select_all, connection);
        return desiredUsers;
    }

    // 对外暴露的添加用户方法，内部处理连接等操作
    public void add(User userToAdd) throws BysjException, SQLException {
        try {
            Connection connection = JdbcHelper.getConn();
            boolean added = add(connection, userToAdd);
            if (added) {
                System.out.println("添加成功");
            } else {
                System.out.println("添加失败");
            }
            JdbcHelper.close(connection);
        } catch (SQLException e) {
            e.printStackTrace();
            throw new BysjException();
        }
    }

    public void login(Connection connection, User userToLogin) throws SQLException {
        try {
            String sql_login = "SELECT * FROM user WHERE username=? and password=?";
            PreparedStatement pstmt_login = connection.prepareStatement(sql_login);
            pstmt_login.setString(1, userToLogin.getUsername());
            pstmt_login.setString(2, userToLogin.getPassword());
        }
        catch (SQLException e) {
            e.printStackTrace();
        }
    }
}